﻿using Corina.Commands;
using Corina.Win.ServerHub.Properties;
using System;
using System.Collections.Generic;
using System.Deployment.Application;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Corina.Win.ServerHub.Viewmodels
{
    public class AboutViewmodel : ViewmodelBase
    {
        public AboutViewmodel()
        {
            ApplicationDeployment.CurrentDeployment.CheckForUpdateCompleted += CheckForUpdateCompleted;
            ApplicationDeployment.CurrentDeployment.UpdateCompleted += UpdateCompleted;
            AutoUpdate = Settings.Default.IDC_AUTOUPDATE;
        }

        private bool _updateAvailable = false;
        public bool UpdateAvailable
        {
            get { return _updateAvailable; }
            set
            {
                _updateAvailable = value;
                RaisePropertyChanged(() => UpdateAvailable);
            }
        }

        private bool _autoUpdate = false;
        public bool AutoUpdate
        {
            get { return _autoUpdate; }
            set
            {
                _autoUpdate = value;
                RaisePropertyChanged(() => AutoUpdate);
                Settings.Default.IDC_AUTOUPDATE = AutoUpdate;
                Settings.Default.Save();
            }
        }

        private void CheckForUpdateCompleted(object sender, CheckForUpdateCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                UpdateAvailable = e.AvailableVersion > CurrentVersion;
                if (UpdateAvailable && AutoUpdate)
                {
                    ApplicationDeployment.CurrentDeployment.UpdateAsync();
                }
            }
        }

        private void UpdateCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if(e.Error==null)
            {
                System.Windows.Forms.Application.Restart();
            }
        }

        public ICommand CheckUpdateCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return ApplicationDeployment.IsNetworkDeployed; },
                    ExecuteCallback = delegate
                    {
                        ApplicationDeployment.CurrentDeployment.CheckForUpdateAsync();
                    }
                };
            }
        }

        public ICommand UpdateCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return UpdateAvailable && !AutoUpdate; },
                    ExecuteCallback = delegate
                    {
                        ApplicationDeployment.CurrentDeployment.UpdateAsync();
                    }
                };
            }
        }
    }
}
